package com.mj.base;

import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.mj.R;
import com.mj.http.NetCallback;
import com.mj.util.DebugLog;

public abstract class BaseFragment extends Fragment {
	public View viewGroup;
	public AQuery aq;
	public BaseActivity baseActivity;
	public ProgressDialog pd;
	private String TAG;
	protected LayoutInflater inflater;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		baseActivity = (BaseActivity) getActivity();
		TAG = this.getClass().getName();
		DebugLog.i(TAG, "onCreate");
		this.inflater = inflater;
		if (container == null) {
			return null;
		}
		if (viewGroup == null) {
			viewGroup = inflater.inflate(getID(), null);
			aq = new AQuery(baseActivity, viewGroup);
			aq.id(R.id.btn_right).clicked(this, "showchoice");
			editView();
		} else {
			((ViewGroup) viewGroup.getParent()).removeView(viewGroup);
		}
		return viewGroup;
	}

	/***
	 * 得到布局ID
	 * 
	 * @return
	 */
	protected abstract int getID();

	/***
	 * 编辑UI
	 */
	protected abstract void editView();

	/***
	 * 回调基类
	 * 
	 * @author duanyimin
	 * 
	 */
	public class baseCallBack implements NetCallback {
		public baseCallBack() {
		}

		@Override
		public void onStart() {
			if (pd == null || !pd.isShowing()) {
				pd = new ProgressDialog(getActivity());
				pd.setMessage("加载中,请稍候");
				pd.setTitle("提示");
				pd.show();
			}
		}

		@Override
		public void onSuccess(JSONObject object) {
			if (pd != null) {
				pd.dismiss();
			}
		}

		@Override
		public void onFailure(String message) {
			if (pd != null) {
				pd.dismiss();
			}
			Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
		}
	}

}
